// 导入axios实例
import httpRequest from '@/http/request'
import { ref } from 'vue'
import { ApiGetTank } from '@/apis/tanks'
import { ElMessage } from 'element-plus'

// 定义接口的传参
interface LoginParm {
    username: string,
    password: string,
    code: string
}
interface RegisterParm {
    username: string,
    password: string,
    password2: string,
    realName: string,
    phone: string,
}

//获取验证码
export function ApiGetimgSrc(loginModel: LoginParm) {
    const imgSrc = ref('')
    let imgStatus = ref(true)
    const changeImg = async () => {
        if (loginModel.username == '') {
            ElMessage.error('请输入用户名')
        } else {
            await httpRequest.get(`/web/user/getCode/${loginModel.username}`, { responseType: 'blob' }).then(res => {
                imgSrc.value = window.URL.createObjectURL(res.data)
                imgStatus.value = false
            })
        }
    }
    return { imgSrc, changeImg, imgStatus }
}

//验证验证码
export async function ApiCheckCode(loginModel: LoginParm) {
    let flag = true;
    await httpRequest({
        url: `/web/user/checkCode/${loginModel.code}/${loginModel.username}`,
        method: 'get'
    }).then(res => {
        if (res.data.code != 0) {
            flag = false;
            ElMessage.error(res.data.message)
        }
    })
    return flag
}

//验证账户密码，并使用sessionStorage记录账号、密码、token、初始鱼缸编号
export async function ApiLogin(loginModel: LoginParm = {username:'',password:'',code:''}) {
    let flag = true;
    let res = await httpRequest({
        url: '/web/user/login',
        method: 'post',
        data: {
            username: loginModel.username,
            password: loginModel.password
        }
    })
    if (res.data.code != 0) {
        console.log(res)
        flag = false;
        ElMessage.error(res.data.message)
        return
    }
    console.log(res)
    localStorage.setItem('token', res.data.data.token);
    sessionStorage.setItem('status', res.data.data.userStatus);
    sessionStorage.setItem('username', loginModel.username);
    sessionStorage.setItem('avater', res.data.data.userInfo.avater);
    sessionStorage.setItem('id', res.data.data.userInfo.id);
    sessionStorage.setItem('email',res.data.data.userInfo.email);
    sessionStorage.setItem('realName',res.data.data.userInfo.realName)
    const { list } = await ApiGetTank(loginModel.username)
    if (list) {
        sessionStorage.setItem('aquarium', list[0]);
    }
    return flag
}


export async function ApiRegister(registerModel: RegisterParm) {
    let flag = true;
    await httpRequest({
        url: '/web/user/register',
        method: 'post',
        data: { ...registerModel }
    }).then((res) => {
        if (res.data.code != 0) {
            flag = false;
            ElMessage.error(res.data.message)
            return
        }
        else {
            ElMessage.success(res.data.message)
            flag = true
        }
    })
    return flag
}